// internal/model/models.go
package model

import (
	"time"
)

// User 用户模型
type User struct {
	ID           int       `json:"id" gorm:"primaryKey;autoIncrement"`
	Username     string    `json:"username" gorm:"size:50;uniqueIndex;not null"`
	PasswordHash string    `json:"-" gorm:"size:255;not null"`
	Email        string    `json:"email" gorm:"size:100;index"`
	Phone        string    `json:"phone" gorm:"size:20"`
	Role         string    `json:"role" gorm:"size:20;default:'user';index"`
	RealName     string    `json:"real_name" gorm:"size:100"`
	Department   string    `json:"department" gorm:"size:100"`
	Avatar       string    `json:"avatar" gorm:"size:500"`
	Status       int       `json:"status" gorm:"default:1"` // 0-禁用 1-正常 2-锁定
	LastLogin    time.Time `json:"last_login"`
	LastIP       string    `json:"last_ip" gorm:"size:45"`
	LoginCount   int       `json:"login_count" gorm:"default:0"`
	CreatedAt    time.Time `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt    time.Time `json:"updated_at" gorm:"autoUpdateTime"`
	CreatedBy    int       `json:"created_by" gorm:"default:0"`
}

// FileMetadata 文件元数据模型
type FileMetadata struct {
	ID            int       `json:"id" gorm:"primaryKey;autoIncrement"`
	UUID          string    `json:"uuid" gorm:"size:36;uniqueIndex;not null"`
	Path          string    `json:"path" gorm:"size:1024;index;not null"`
	Name          string    `json:"name" gorm:"size:255;not null"`
	Size          int64     `json:"size" gorm:"default:0"`
	IsDir         bool      `json:"is_dir" gorm:"default:false"`
	FileExt       string    `json:"file_ext" gorm:"size:20;index"`
	MimeType      string    `json:"mime_type" gorm:"size:100"`
	ModTime       time.Time `json:"mod_time"`
	CreateTime    time.Time `json:"create_time" gorm:"autoCreateTime"`
	OwnerID       int       `json:"owner_id" gorm:"index;not null"`
	Owner         User      `json:"owner" gorm:"foreignKey:OwnerID"`
	Permissions   string    `json:"permissions" gorm:"size:10;default:'644'"`
	ParentID      int       `json:"parent_id" gorm:"index;default:0"`
	Checksum      string    `json:"checksum" gorm:"size:64;index"`
	ThumbnailPath string    `json:"thumbnail_path" gorm:"size:1024"`
	PreviewPath   string    `json:"preview_path" gorm:"size:1024"`
	PreviewStatus int       `json:"preview_status" gorm:"default:0"` // 0-未生成 1-生成中 2-已生成 3-失败
	Version       int       `json:"version" gorm:"default:1"`
	Tags          string    `json:"tags" gorm:"type:text"` // JSON数组
	Description   string    `json:"description" gorm:"type:text"`
	IsEncrypted   bool      `json:"is_encrypted" gorm:"default:false"`
	EncryptionKey string    `json:"-" gorm:"size:64"`
	Deleted       bool      `json:"deleted" gorm:"default:false;index"`
	DeletedAt     *time.Time `json:"deleted_at"`
	DeletedBy     int       `json:"deleted_by" gorm:"default:0"`
}

// Permission 权限模型
type Permission struct {
	ID          int    `json:"id" gorm:"primaryKey;autoIncrement"`
	Path        string `json:"path" gorm:"size:1024;index;not null"`
	UserID      *int   `json:"user_id" gorm:"index"`
	Role        string `json:"role" gorm:"size:20;index"`
	CanView     bool   `json:"can_view" gorm:"default:false"`
	CanDownload bool   `json:"can_download" gorm:"default:false"`
	CanEdit     bool   `json:"can_edit" gorm:"default:false"`
	CanDelete   bool   `json:"can_delete" gorm:"default:false"`
	CanShare    bool   `json:"can_share" gorm:"default:false"`
	CanPreview  bool   `json:"can_preview" gorm:"default:false"`
	Priority    int    `json:"priority" gorm:"default:0;index"`
	ExpiresAt   *time.Time `json:"expires_at"`
	CreatedAt   time.Time `json:"created_at" gorm:"autoCreateTime"`
	CreatedBy   int       `json:"created_by" gorm:"default:0"`
}

// AuditLog 审计日志模型
type AuditLog struct {
	ID        int       `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID    int       `json:"user_id" gorm:"index;not null"`
	User      User      `json:"user" gorm:"foreignKey:UserID"`
	Action    string    `json:"action" gorm:"size:50;index;not null"`
	Path      string    `json:"path" gorm:"size:1024"`
	FileID    *int      `json:"file_id"`
	Details   string    `json:"details" gorm:"type:text"`
	IP        string    `json:"ip" gorm:"size:45"`
	UserAgent string    `json:"user_agent" gorm:"size:500"`
	Status    string    `json:"status" gorm:"size:20;default:'success'"` // success, failed
	Timestamp time.Time `json:"timestamp" gorm:"autoCreateTime;index"`
}

// ShareLink 分享链接模型
type ShareLink struct {
	ID          int       `json:"id" gorm:"primaryKey;autoIncrement"`
	UUID        string    `json:"uuid" gorm:"size:36;uniqueIndex;not null"`
	FileID      int       `json:"file_id" gorm:"index;not null"`
	File        FileMetadata `json:"file" gorm:"foreignKey:FileID"`
	CreatedBy   int       `json:"created_by" gorm:"index;not null"`
	Password    string    `json:"password" gorm:"size:100"`
	ExpiresAt   time.Time `json:"expires_at" gorm:"index"`
	MaxDownloads int      `json:"max_downloads" gorm:"default:0"`
	DownloadCount int     `json:"download_count" gorm:"default:0"`
	IsActive    bool      `json:"is_active" gorm:"default:true"`
	CreatedAt   time.Time `json:"created_at" gorm:"autoCreateTime"`
}

// Tag 标签模型
type Tag struct {
	ID        int       `json:"id" gorm:"primaryKey;autoIncrement"`
	Name      string    `json:"name" gorm:"size:50;uniqueIndex;not null"`
	Color     string    `json:"color" gorm:"size:7;default:'#007bff'"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
	CreatedBy int       `json:"created_by" gorm:"default:0"`
}

// FileTag 文件标签关联模型
type FileTag struct {
	ID     int `json:"id" gorm:"primaryKey;autoIncrement"`
	FileID int `json:"file_id" gorm:"uniqueIndex:idx_file_tag"`
	TagID  int `json:"tag_id" gorm:"uniqueIndex:idx_file_tag"`
}

// SearchHistory 搜索历史模型
type SearchHistory struct {
	ID        int       `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID    int       `json:"user_id" gorm:"index;not null"`
	Keyword   string    `json:"keyword" gorm:"size:200;not null"`
	Results   int       `json:"results" gorm:"default:0"`
	Timestamp time.Time `json:"timestamp" gorm:"autoCreateTime;index"`
}

// SystemConfig 系统配置模型
type SystemConfig struct {
	ID    string `json:"id" gorm:"primaryKey;size:50"`
	Value string `json:"value" gorm:"type:text"`
	Desc  string `json:"desc" gorm:"size:200"`
}